<?php
function personal_id_check($body) {
  $wi = array(7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2);
  $ai = array('1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2');
  $sigma = 0;
  for ($i=0; $i<17; $i++)
    $sigma += ((int)$body{$i})*$wi[$i];
  return substr($body,strlen($body)-1)==$ai[$sigma%11];
}

class ServerDB {

  public $link, $qstr;
  
  public function __construct() {
    $this->link = mysql_connect("localhost", "judge", "vXCurxp8K.F,j5Ne");
  }
  public function __destruct() {
    mysql_close($this->link);
  }
  public function update($ojid, $name, $pid, $sid, $email, $is_hust) {
    $this->qstr=sprintf("UPDATE `judge`.`alibaba` SET `name`='%s', `stdnum`='%s', `id`='%s', `school`=%s, `email`='%s' WHERE `ojid`='%s';",       
      mysql_real_escape_string($name),
      mysql_real_escape_string($sid),
      mysql_real_escape_string($pid),
      mysql_real_escape_string($is_hust),
      mysql_real_escape_string($email),
      mysql_real_escape_string($ojid)
    );
    return mysql_query($this->qstr, $this->link);
    return mysql_affected_rows($this->link);
  }
  public function insert($ojid, $name, $pid, $sid, $email, $is_hust) {
    $this->qstr=sprintf("INSERT INTO `judge`.`alibaba` (`ojid`,`name`,`stdnum`,`id`,`school`,`email`) VALUE ('%s','%s','%s','%s',%s,'%s');",
      mysql_real_escape_string($ojid),
      mysql_real_escape_string($name),
      mysql_real_escape_string($sid),
      mysql_real_escape_string($pid),
      mysql_real_escape_string($is_hust),
      mysql_real_escape_string($email)
    );
#	echo $this->qstr;
#	exit;
    return mysql_query($this->qstr, $this->link);
#	echo mysql_error($this->link);
#   exit;
    return mysql_affected_rows($this->link);
  }
  
  public function query_pid($pid) {
    $this->qstr=sprintf("SELECT * FROM `judge`.`alibaba` WHERE `id`='%s';", mysql_real_escape_string($pid));
    return mysql_query($this->qstr, $this->link);

  }
  public function query_sid($sid) {
    $this->qstr=sprintf("SELECT * FROM `judge`.`alibaba` WHERE `stdnum`='%s';", mysql_real_escape_string($sid));
    return mysql_query($this->qstr, $this->link);
  }
  public function query_ojid($ojid) {
    $this->qstr=sprintf("SELECT * FROM `judge`.`alibaba` WHERE `ojid`='%s';", mysql_real_escape_string($ojid));
    return mysql_query($this->qstr, $this->link);
  
	}
  public function query_email($email) {
	$this->qstr=sprintf("SELECT * FROM `judge`.`alibaba` WHERE `email`='%s';", mysql_real_escape_string($email));
	return mysql_query($this->qstr, $this->link);
  }
  public function query($qstr) {
    $this->qstr=$qstr;
    return mysql_query($this->qstr, $this->link);
  }
}

$DB = new ServerDB();

if(isset($_REQUEST["get"])) {
  session_start();
  if(!isset($_SESSION["user_id"]))
    echo json_encode(array("statues"=>0));
  else {
  
    $result=$DB->query_ojid($_SESSION["user_id"]);
    if($result && mysql_num_rows($result)==0) echo json_encode( array("statues"=>1, "ojid"=>$_SESSION["user_id"]));
    else if($result && mysql_num_rows($result)){
      $row=mysql_fetch_assoc($result);
      echo json_encode( array(
		"statues"=>2,
        "ojid"=>$row["ojid"], 
        "sid"=>$row["stdnum"], 
        "pid"=>$row["id"], 
        "email"=>$row["email"], 
        "name"=>$row["name"], 
        "is_hust"=>$row["school"]));
    }
  }
  session_write_close();
  
} else if(isset($_REQUEST["check"])) {
  list($ojid,$name,$sid,$pid,$is_hust,$email)=
  array(
    $_POST["ojid"],
    $_POST["name"],
    $_POST["sid"],
    $_POST["pid"],
    $_POST["is_hust"]=="Yes"?1:0,
    $_POST["email"]
  );
  
  $ret = array();

  /* check pid*/
  $result=$DB->query_pid($pid);
  if($result && mysql_num_rows($result)) {
    $row=mysql_fetch_assoc($result);
    if($row["ojid"] != $ojid) $ret["pid"]=1;
  }
  else if(!personal_id_check($pid)) $ret["pid"]=2;

  /*check sid */
  if((int)$is_hust == 1) {
    $result=$DB->query_sid($sid);
    if($result && mysql_num_rows($result)) {
      $row=mysql_fetch_assoc($result);
      if($row["ojid"] != $ojid) $ret["sid"]=1;
    }
  }

  /* check email */
  $result=$DB->query_email($email);
  if($result && mysql_num_rows($result)){
	$row=mysql_fetch_assoc($result);
	if($row["ojid"]!=$ojid) $ret["email"]=1;
  }
  echo json_encode($ret);
  
} else if(isset($_REQUEST["submit"])) {
  list($ojid,$name,$sid,$pid,$is_hust,$email)=
  array(
    $_POST["ojid"],
    $_POST["name"],
    $_POST["sid"],
    $_POST["pid"],
    $_POST["is_hust"]=="Yes"?1:0,
    $_POST["email"]
  );
  session_start();
  if($ojid!=$_SESSION["user_id"] && isset($_SESSION["user_id"])) echo json_encode(array("error"=>"[]Onlinejudge ID"));
  else {
	  $result=$DB->query_ojid($ojid);
	  if($result && mysql_num_rows($result))
	    $result=$DB->update($ojid, $name, $pid, $sid, $email, $is_hust);
	  else {
		$result=$DB->insert($ojid, $name, $pid, $sid, $email, $is_hust);
		if($result) {
		
		  $qstr = sprintf("INSERT INTO `judge`.`privilege`(`user_id`,`rightstr`) VALUE ('%s','c1079');", mysql_real_escape_string($ojid));
		  mysql_query($qstr, $DB->link);
		}
	  }
	  if($result) {

		  echo json_encode(array("success"=>1));
	  }
	  else {
		$ret = array();

	    /* check pid*/
		$result=$DB->query_pid($pid);
	    if($result && mysql_num_rows($result)) {
	      $row=mysql_fetch_assoc($result);
	      if($row["ojid"] != $ojid) $ret["pid"]=1;  
	    }
		else if(!personal_id_check($pid)) $ret["pid"]=2;
    
	    /*check sid */
	    if((int)$is_hust == 1) {
	      $result=$DB->query_sid($sid);
	      if($result) {
	        $row=mysql_fetch_assoc($result);
	        if($row["ojid"] != $ojid) $ret["sid"]=1;
	      }
	    }
	    /* check email */
		
		$result=$DB->query_email($email);
		if($result && mysql_num_rows($result)){
			$row=mysql_fetch_assoc($result);
			if($row["ojid"] != $ojid) $ret["email"]=1;
		}
	    echo json_encode($ret);
	  }
  }
  session_write_close();
}

